import { Component, OnInit, Output, EventEmitter, Input } from '@angular/core';
import { appKeywordUrl } from '../../keyword.url';
import { CudTableService } from 'app/public/serve/cud-table/cud-table.service';
import { Service } from '../../../server/server';
import { keyword } from '../../keyword.data';
import { HintsService } from '../../../public/serve/hints/hints.service';
import { InitService } from '../../../public/serve/init/init.service';
import { DatePipe } from '@angular/common';
import {appSynonymUrl} from '../../../synonym/synonym.url';

declare var $: any;

@Component({
  selector: 'app-trial-edit',
  templateUrl: './trial-edit.component.html'
})
export class TrialEditComponent implements OnInit {

  @Output() onSubmit = new EventEmitter<void>();

  details: object = {};
  keywordListVal : Array<object>;
  @Input() themeTagListVal: any;

  constructor(
    private server: Service,
    public initService: InitService,
    private request: CudTableService,
    private hintsService: HintsService,
    private datePipe: DatePipe,
  ) { }

  ngOnInit() {}

  _list(){
  
    let data = this._tableData();
    this.request.postRequest(this.server.baseUrl +appKeywordUrl.getThemeWordsList,data).then((res:any) => {
      if (res.result_code === '0000') {
        this.keywordListVal = res.result;
      }else{
        this.hintsService.messages(res.message,res.result_code);
      }
      return;
    })
    .catch((err: any)=>{
      this.hintsService.messages("error","error");
      return;
    });
  }

  /**
   * 主题词详情
   * @param info
   */
  _details(info: object){
    this._list();
    this.details = info;
  }

  /**
   * 编辑保存
   */
  _onSubmit(info){
    info.token = keyword.retrieval.token;
    info.createBy = this.server.getuserName();
    info.createTime = this.datePipe.transform(new Date(), 'yyyy-MM-dd HH:mm:ss');
    info.checkStatus = keyword.retrieval.checkStatus;
    info.child = 0;
    info.themeTableId = keyword.retrieval.themeTableId;
    info.themeGroupId = keyword.retrieval.themeGroupId;

    this.request
    .postRequest(this.server.baseUrl + appKeywordUrl.updateThemeWords,info)
    .then((res: any)=>{
     
      if(res.result_code === '0000'){
        this._editInit();
      }

      this.hintsService.messages(res.message,res.result_code);
      return;
    })
    .catch(err=>{
      this.hintsService.messages("error","error");
      this._editInit();
      return;
    });
  }

  /**
   * 初始化
   */
  private _editInit(){
    this.initService._init();
    $("#edit").modal("hide");
    this.onSubmit.emit();
  }

   //查询参数
   private _tableData(){
    return {
      token         : keyword.retrieval.token,
      pageNo        : keyword.pagination.pageNo,
      pageSize      : keyword.pagination.pageSize,
      status        : keyword.retrieval.status,
      themeCode     : keyword.retrieval.themeCode,
      checkStatus   : keyword.retrieval.checkStatus,
      startTime     : keyword.retrieval.startTime,
      endTime       : keyword.retrieval.endTime,
      queryCriteria : keyword.retrieval.queryCriteria,
      themeTableId  : keyword.retrieval.themeTableId
    }
  }

}
